<?php
/**
 * 移动端客源控制器
 * User: Wu Ning
 * Date: 2018-08-21
 * Time: 18:05
 */
namespace App\Http\Controllers\Mobile\Customer;
use App\Http\Controllers\Controller;
use App\Tool\Error;
use Illuminate\Http\Request;
use App\Model\Backstage\Customers;
use Validator;
class CustomersController extends Controller
{
   /**
    * 移动端客源列表
    * Wu Ning
    */
    public function CustomerList(Request $request,Customers $customers){
        $parameter = $request->all();
        $where = [];
        $where[] = ['c_status', '=', 1];//过滤非回收站房源
        $where[] = ['c_status', '=', 1];//过滤非回收站房源
        //获取公司的ID
//        $branchId = 2;
//        $where[] = ['c_branch_id', '=', $branchId];
        //二级区域ID
        if ($request->input('c_area_id')){
            $where[] = ['c_area_id', 'like', '%'.$parameter['c_area_id'].'%'];
        }
        //三级区域ID
        if ($request->input('c_secondary_area')){
            $where[] = ['c_secondary_area', 'like', '%'.$parameter['c_secondary_area'].'%'];
        }
        //房型
        if ($request->input('c_room')){
            $where[] = ['c_room', '=', $parameter['c_room']];
        }
        //交易类型
        if ($request->input('c_trading_type')){
            $where[] = ['c_trading_type', '=', $parameter['c_trading_type']];
        }
        //客源状态
        if ($request->input('c_buy_status')){
            $where[] = ['c_buy_status', '=', $parameter['c_buy_status']];
        }
        //用途
        if ($request->input('c_use_type')){
            $where[] = ['c_use_type', '=', $parameter['c_use_type']];
        }
        //日期 1：委托日期   2：最后跟进日期   3：首次录入时间  前台直接传时间戳

        //委托时间
        if ($request->input('date_type') == 1){
            if ($parameter['start_time']) {
                $where[] = ['c_entrust_time', '>=', $parameter['start_time']];
            }
            if ($parameter['end_time']) {
                $where[] = ['c_entrust_time', '<=', $parameter['end_time']];
            }
        }
        //最后跟进时间
        if ($request->input('date_type') == 2){
            if ($parameter['start_time']) {
                $where[] = ['c_followup_time', '>=', $parameter['start_time']];
            }
            if ($parameter['end_time']) {
                $where[] = ['c_followup_time', '<=', $parameter['end_time']];
            }
        }
        //首次录入时间
        if ($request->input('date_type') == 3){
            if ($parameter['start_time']) {
                $where[] = ['c_created_at', '>=', $parameter['start_time']];
            }
            if ($parameter['end_time']) {
                $where[] = ['c_created_at', '<=', $parameter['end_time']];
            }
        }
        //房屋类型
        if ($request->input('c_room_type')){
            $where[] = ['c_room_type', '=', $parameter['c_room_type']];
        }
        //房屋朝向
        if ($request->input('c_toward')){
            $where[] = ['c_toward', '=', $parameter['c_toward']];
        }
        //房屋价格 最小值
        if ($request->input('c_price_min')){
            $where[] = ['c_price_min', '>=', $parameter['c_price_min']];
        }
        //房屋价格 最大值
        if ($request->input('c_price_max')){
            $where[] = ['c_price_max', '<=', $parameter['c_price_max']];
        }
        //房屋面积 最小值
        if ($request->input('c_acreage_min')){
            $where[] = ['c_acreage_min', '>=', $parameter['c_acreage_min']];
        }
        //房屋面积 最大值
        if ($request->input('c_acreage_max')){
            $where[] = ['c_acreage_max', '<=', $parameter['c_acreage_max']];
        }
        //公私类型
        if ($request->input('c_customers_type')){
            $where[] = ['c_customers_type', '=', $parameter['c_customers_type']];
        }
        //门店ID
        if ($request->input('c_store_id')){
            $where[] = ['c_store_id', '=', $parameter['c_store_id']];
        }
        //员工ID
        if ($request->input('c_employees_id')){
            $where[] = ['c_employees_id', '=', $parameter['c_employees_id']];
        }
        //智能查询(keywords)   客户名字   客户手机号  地址
        $keywords = '';
        if ($request->input('keywords')){
            $keywords = trim($request->input('keywords'));
        }
        $list = $customers->listCustomers(env('PAGE_LIMIT_ADMIN'), $where, '');
        if (!$list) {
            return $this->error(Error::NULL_DATA);
        }

        return $list;
    }

    /**
     * 移动端添加客源信息
     * Wu Ning
     */
    public function addcustomers(Request $request){
        $params = $request->all();
        $validator = Validator::make($params, [
            'c_user_name'   => 'required',
            'c_user_phone'   => 'required',
            'c_trading_type'   => 'required',
            'c_buy_status'       => 'required',
            'c_area_id'   => 'required',
            'c_secondary_area'       => 'required',
            'c_room'   => 'required',
            'c_hall'   => 'required',
            'c_toilet'        => 'required',
            'c_acreage_max'   => 'required',
            'c_acreage_min'   => 'required',
            'c_price_max'   => 'required',
            'c_price_min'   => 'required',
            'c_entrust_time'   => 'required',
            'c_entrust_type'   => 'required',
            'c_user_level'   => 'required',
            'c_use_type'   => 'required',
            'c_floor'   => 'required'
         ]);
        if ($validator->fails()) {
            return $this->error(Error::PARAM_MISSING);
        }

        $customers = new Customers();
        $res = $customers->addCustomers($params);
        if (!$res) {
            return $this->error(Error::ADD_ERROR);
        }
        return $this->success('ok', '添加成功');
    }

    /**
     * 添加计划任务
     */
    public function addtask(Request $request){
        $params = $request->all();
        $validator = Validator::make($params, [
            'hmp_room_customer'   => 'required',
            'hmp_room_customer_id'   => 'required',
            'hmp_sign_type'   => 'required',
            'hmp_status'   => 'required',
            'hmp_plan_time'   => 'required'
        ]);
        $params['hmp_created_time'] = time();
        $params['hmp_update_time'] = time();
        if ($validator->fails()) {
            return $this->error(Error::PARAM_MISSING);
        }
        $customers = new Customers();
        $res = $customers->addplan($params);
        if (!$res) {
            return $this->error(Error::ADD_ERROR);
        }
        return $this->success('ok', '添加成功');

    }

    //个人信息
    public function personalInfo(){
        $customers = new Customers();
        $res = $customers->getPersonalInfo();
        if(!$res){
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok',$res);
    }
}
